using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace UI
{
    public class ChangeSceneButton : MonoBehaviour
    {
        [SerializeField] private SceneTransition sceneTransition;

        [SerializeField] private int targetSceneIndex = 0;
        
        private Button _button;


        private void Awake()
        {
            _button = GetComponent<Button>();
            
            _button.onClick.AddListener(Button_Clicked);
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Return))
            {
                Button_Clicked();
            }
        }

        private void Button_Clicked()
        {
            sceneTransition.FadeOut(actionOnFaded: LoadScene);
            enabled = false;
            _button.onClick.RemoveListener(Button_Clicked);
        }

        private void LoadScene()
        {
            SceneManager.LoadScene(targetSceneIndex);
        }
    }
}